use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;

#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct Plan {
    pub id: i64,
    pub user_id: i64,
    pub name: String,
    pub description: String,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub status: String,  // 'pending' or 'completed'
    pub frequency: String,  // 'daily', 'weekly', 'monthly', 'yearly', 'once'
    pub next_reminder: DateTime<Utc>,
    pub last_sent_at: Option<DateTime<Utc>>,  // 最后一次发送时间，防止重复发送
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PlanStatus {
    Pending,
    Completed,
}

impl PlanStatus {
    pub fn as_str(&self) -> &str {
        match self {
            PlanStatus::Pending => "pending",
            PlanStatus::Completed => "completed",
        }
    }
    
    pub fn from_str(s: &str) -> Option<Self> {
        match s {
            "pending" => Some(PlanStatus::Pending),
            "completed" => Some(PlanStatus::Completed),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Frequency {
    Daily,
    Weekly,
    Monthly,
    Yearly,
    Once,
}

impl Frequency {
    pub fn as_str(&self) -> &str {
        match self {
            Frequency::Daily => "daily",
            Frequency::Weekly => "weekly",
            Frequency::Monthly => "monthly",
            Frequency::Yearly => "yearly",
            Frequency::Once => "once",
        }
    }
    
    pub fn from_str(s: &str) -> Option<Self> {
        match s {
            "daily" => Some(Frequency::Daily),
            "weekly" => Some(Frequency::Weekly),
            "monthly" => Some(Frequency::Monthly),
            "yearly" => Some(Frequency::Yearly),
            "once" => Some(Frequency::Once),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreatePlan {
    pub user_id: i64,
    pub name: String,
    pub description: String,
    pub frequency: String,
    pub next_reminder: DateTime<Utc>,
}
